package yl.hs.bmipfull.service.equipment.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import yl.hs.bmipfull.daos.equipment.IDeviceRepairDao;
import yl.hs.bmipfull.pojo.equipment.DeviceInspectionEntity;
import yl.hs.bmipfull.pojo.equipment.DeviceMaintainEntity;
import yl.hs.bmipfull.pojo.equipment.DeviceRepairEntity;
import yl.hs.bmipfull.pojo.equipment.EquipmentEntity;
import yl.hs.bmipfull.service.equipment.IDeviceRepairService;
import yl.hs.bmipfull.utils.HsApp;
import yl.hs.bmipfull.utils.SearchFilterEngine;
import yl.hs.bmipfull.utils.Tl;
import yl.hs.bmipfull.utils.datastate.ActionType;
import yl.hs.bmipfull.viewmodels.PageQuery;
import yl.hs.bmipfull.viewmodels.PageResult;
import yl.hs.bmipfull.viewmodels.SubmitModel;

/*
 * DeviceRepairServiceImpl
 * @category
 * @author 谢景广
 * @date 2022-11-15
 */
@Service
public class DeviceRepairServiceImpl extends EquipmentBaseService implements IDeviceRepairService {

    @Override
    public PageResult<DeviceRepairEntity> queryPage(PageQuery query, int rowNum, int page) {
        PageResult<DeviceRepairEntity> result = new PageResult(rowNum);
        var filters = SearchFilterEngine.generateQueryList(query.getFilters(),
                DeviceRepairEntity.class);
        result.setPage(page);
        query.getQuery().put("querySkip", (page - 1) * rowNum);
        query.getQuery().put("pageSize", rowNum);
        query.checkSortSides(DeviceRepairEntity.class);
        query.getQuery().put("sortSide", String.join(",",query.getSortSides()));
        result.setRecords(repairDao.queryCountByCondition(query.getQuery(),filters));
        result.setData(repairDao.queryListByCondition(query.getQuery(),filters));
        return result;
    }

    @Override
    public Integer save(SubmitModel model) throws Exception {
        var result = 0;
        DeviceRepairEntity entity;
        if(Tl.isEmpty(model.getSn())){
            entity = new DeviceRepairEntity();
            Tl.getChangeLog(entity,model.getData());
            entity.setSn(queryNewSN(entity.getClass()));
            result = repairDao.insertNew(entity);
            writeLog(ActionType.CREATE,entity.getSn(),"",null,entity.getClass());
            writeLog(ActionType.CREATE,entity.getEquipmentSN(),"%s创建新的编号为"+entity.getSn()+"的维修信息",EquipmentEntity.class);
        }
        else{
            entity = repairDao.findBySN(model.getSn());
            var backJson = entity.toJson();
            var changeLog = Tl.getChangeLog(entity,model.getData());
            writeLog(ActionType.UPDATE,entity.getSn(),changeLog,backJson,entity.getClass());
            writeLog(ActionType.UPDATE,entity.getEquipmentSN(),"%s更新了编号为"+entity.getSn()+"的保养信息:"+changeLog,EquipmentEntity.class);
            result = repairDao.update(entity);
        }
        if(result>0){
            var user = HsApp.getCurrent();
            saveGS(
                    entity.getSn(),
                    entity.getEquipmentName(),
                    String.join(",",
                            entity.getEquipmentName(),
                            entity.getHandlerName(),
                            entity.getItems(),
                            entity.getRemark()
                    ),
                    entity,
                    HsApp.getCurrent() != null ? HsApp.getCurrent().getUserSN() : null,
                    user.getOrganizationCode()
            );
        }
        return result;
    }

    @Override
    public Integer delete(String[] snArray) {
        var result = repairDao.changeState(snArray,-1);
        if(result> 0){
            var deleteList = repairDao.queryListBySNArray(snArray);
            for (var del : deleteList){
                writeLog(ActionType.DELETE, del.getEquipmentSN(),"%s删除了设备编号为"+del.getSn()+"的维修信息","", EquipmentEntity.class);
            }
            deleteGs(snArray);
        }
        return  result;
    }

    @Override
    public DeviceRepairEntity findBySN(String sn) {
        return repairDao.findBySN(sn);
    }

    private IDeviceRepairDao repairDao;

    @Autowired
    public void setRepairDao(IDeviceRepairDao repairDao) {
        this.repairDao = repairDao;
    }


}
